home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_5 / issue_08 / boota / C-source / c / argfuncs < prev    next >
Encoding:
Text File  |  1991-12-02  |  10.7 KB  |  272 lines

  1. /* > c.argfuncs - (c) Paul Witheridge 1990 Version 1.0               */
  2.  
  3. /*===================================================================*/
  4. /*                                                                   */
  5. /*  This file includes the following functions:                      */
  6. /*                                                                   */
  7. /*       arganal      -   analyse standard arguments                 */
  8. /*       displaytext  -   display help text etc                      */
  9. /*                                                                   */
  10. /*===================================================================*/
  11.  
  12. #include "kernel.h"                   /* ARC specifics               */
  13. #include "swis.h"                     /* SWI names                   */
  14.  
  15. #include <ctype.h>                    /* Character handling          */
  16. #include <stddef.h>                   /* Standard definitions        */
  17. #include <stdlib.h>                   /* General utilities           */
  18. #include <stdio.h>                    /* Input/output                */
  19. #include <string.h>                   /* String handling             */
  20.  
  21. #include "GetDirs.h"                  /* Defines Beep func           */
  22. #include "Useful.h"                   /* Useful defs                 */
  23. #include "Argfuncs.h"                 /* For checking                */
  24.  
  25. /*===================================================================*/
  26. /*                                                                   */
  27. /*  analargs  -  analyse command line arguments                      */
  28. /*  --------                                                         */
  29. /*                                                                   */
  30. /*  This function analyses standard command line arguments which can */
  31. /*  be any combination of the following three types:                 */
  32. /*                                                                   */
  33. /*  (1) positional arguments (cannot begin with '-')                 */
  34. /*  (2) option switches (single character preceded by '-')           */
  35. /*  (3) option value (follows option switch, cannot begin with '-')  */
  36. /*                                                                   */
  37. /*  The arguments provided by the caller comprise:                   */
  38. /*                                                                   */
  39. /*   -  count of command line arguments                              */
  40. /*   -  array of ptrs to command line arguments                      */
  41. /*   -  max number of position arguments allowed                     */
  42. /*   -  array of ptrs to ptrs for storing address of pos. args       */
  43. /*   -  string of allowable option characters (upper case and        */
  44. /*      terminated by null byte)                                     */
  45. /*   -  pointer to flag byte                                         */
  46. /*   -  array of flag masks (unsigned char) for setting flags in     */
  47. /*      flag byte corresponding to option characters                 */
  48. /*   -  array of ptrs to ptrs for storing address of option value    */
  49. /*      args                                                         */
  50. /*                                                                   */
  51. /*===================================================================*/
  52.  
  53. void analargs(
  54.   const int argc,                     /* Number of arguments         */
  55.   const char *const argv[],           /* Array of pointers to args   */
  56.   const int posnc,                    /* Number of positional args   */
  57.   const char **posnptr[],             /* Ptr to positional arg ptrs  */
  58.   const char options[],               /* Option characters           */
  59.   char *flags,                        /* Pointer to flag byte        */
  60.   const char optflags[],              /* Option flag settings        */
  61.   const char **optval[])              /* Ptrs to value ptrs          */
  62. {
  63.   /*-----------------------------------------------------------------*/
  64.   /*  Local definitions.                                             */
  65.   /*-----------------------------------------------------------------*/
  66.  
  67.   int i ;                             /* Argument index              */
  68.   int j ;                             /* Option index                */
  69.   int k ;                             /* Positional argument index   */
  70.   const char *p ;                     /* Working pointer             */
  71.  
  72.   /*-----------------------------------------------------------------*/
  73.   /*  Executable statements                                          */
  74.   /*                                                                 */
  75.   /*  Loop through arguments.                                        */
  76.   /*                                                                 */
  77.   /*  Multiple positional operands allowed up to maximum number      */
  78.   /*  specified by caller - recognised because they do not start     */
  79.   /*  with '-' and are not preceded by an option character which     */
  80.   /*  takes a value.                                                 */
  81.   /*                                                                 */
  82.   /*  Multiple option arguments allowed - recognised because they    */
  83.   /*  do begin with '-' which must be followed by a single           */
  84.   /*  character which must be one of the charcters in the "options"  */
  85.   /*  array and specifies the option selected.                       */
  86.   /*                                                                 */
  87.   /*  Option can set a global flag - flag setting specified by the   */
  88.   /*  corresponding entry in the "optflags" array.                   */
  89.   /*                                                                 */
  90.   /*  Option may be followed by a value if corresponding pointer     */
  91.   /*  in "optvals" array is non-null. Pointer points to actual       */
  92.   /*  pointer which must be set to point to the value.               */
  93.   /*                                                                 */
  94.   /*-----------------------------------------------------------------*/
  95.  
  96.   k = 0 ;
  97.  
  98.   for ( i = 1 ; i < argc ; i++ )
  99.   {
  100.     if ( argv[i][0] == '-' )
  101.     {
  102.       if ( (p = strchr(options,toupper(argv[i][1]))) == NULL )
  103.       {
  104.         goto badopts ;
  105.       }
  106.       j = p - options ;
  107.       *flags |= optflags[j] ;
  108.       if ( optval[j] == NULL )
  109.       {
  110.         if ( argv[i][2] != '\0' )
  111.         {
  112.           goto badopts ;
  113.         }
  114.       }
  115.       else
  116.       {
  117.         if ( argv[i][2] == '\0' )
  118.         {
  119.           i++ ;
  120.           if ( i < argc && argv[i][0] != '-' )
  121.           {
  122.             *optval[j] = argv[i] ;
  123.           }
  124.           else
  125.           {
  126.             i-- ;
  127.           }
  128.         }
  129.         else
  130.         {
  131.           *optval[j] = &argv[i][2] ;
  132.         }
  133.       }
  134.     }
  135.     else
  136.     {
  137.       if ( k < posnc )
  138.       {
  139.         *posnptr[k++] = argv[i] ;
  140.       }
  141.       else
  142.       {
  143.         p = "operand" ;
  144.         goto badargs ;
  145.       }
  146.     }
  147.   }
  148.  
  149.   return ;
  150.   
  151.   /*-----------------------------------------------------------------*/
  152.   /*  If bad argument found sound beep and exit with code 8          */
  153.   /*-----------------------------------------------------------------*/
  154.  
  155.   badopts:
  156.     
  157.     p = "option" ;
  158.  
  159.   badargs:
  160.     
  161.     printf("ERROR - invalid %s: %s\n\n",p,argv[i]) ;
  162.     beep() ;
  163.     exit(8) ;
  164. }
  165.  
  166. /*===================================================================*/
  167. /*                                                                   */
  168. /*  displaytext  -   display help/syntax text                        */
  169. /*  -----------                                                      */
  170. /*                                                                   */
  171. /*  This function displays text according to the current text        */
  172. /*  window width. It is passed pointers to a text string and an      */
  173. /*  array of tab settings.                                           */
  174. /*                                                                   */
  175. /*  Control characters (codes less than 0x20) have the following     */
  176. /*  special effects:                                                 */
  177. /*                                                                   */
  178. /*    0x00       =  end of string                                    */
  179. /*                                                                   */
  180. /*    0x01-0x1e  =  indices into tab settings array                  */
  181. /*                                                                   */
  182. /*    0x1f       =  new line required                                */
  183. /*                                                                   */
  184. /*  The array of tab settings is an array of unsigned characters.    */
  185. /*  The first character is the indent to be used at the start of     */
  186. /*  each line. Other characters are indexed by characters 0x01 to    */
  187. /*  0x1e in the text.                                                */
  188. /*                                                                   */
  189. /*===================================================================*/
  190.  
  191. void displaytext(
  192.   const char *text,                   /* Ptr to help text            */
  193.   const unsigned char tabs[])         /* Ptr to array of tab values  */
  194.  
  195. {
  196.   int c ;                             /* Working character           */
  197.   int i = 0 ;                         /* Offset into current line    */
  198.   int b ;                             /* Count of blanks             */
  199.   int t ;                             /* Current indent              */ 
  200.   int l ;                             /* Line length                 */
  201.   const char *p ;                     /* Working pointer             */
  202.  
  203.   static const int varkeys[] =
  204.   {
  205.     132,
  206.     134,
  207.     -1
  208.   } ;
  209.  
  210.   int varvals[2] ;
  211.  
  212.   _kernel_swi_regs swiregs ;
  213.  
  214.   swiregs.r[0] = (int)varkeys ;
  215.   swiregs.r[1] = (int)varvals ;
  216.   _kernel_swi(OS_ReadVduVariables,&swiregs,&swiregs) ;
  217.   
  218.   l = varvals[1] - varvals[0] ;
  219.  
  220.   b = t = tabs[0] ;
  221.  
  222.   while ( (c = *text) != '\0' )
  223.   {
  224.     if ( c <= 0x20 )
  225.     {
  226.       switch ( c )
  227.       {
  228.         case 0x20 :             /* Process blank                     */
  229.  
  230.           b++ ;
  231.           break;
  232.  
  233.         case 0x1f :             /* Process new line                  */
  234.     
  235.           putchar('\n') ;
  236.           i = 0 ;
  237.           t = b = tabs[0] ;
  238.           break;
  239.  
  240.         default :               /* Process tab                       */
  241.     
  242.           t = tabs[c] ;
  243.           if ( t > i + b )
  244.           {
  245.             b = t - i ;
  246.           }
  247.       }
  248.       text++ ;
  249.     }
  250.     else
  251.     {
  252.       for ( p = text ; *p > 0x20 ; p++ ) ;
  253.       if ( i + b + p - text > l )
  254.       {
  255.         putchar('\n') ;
  256.         i = 0 ;
  257.         b = t ;
  258.       }
  259.       for ( ; b > 0 ; b--, i++ )
  260.       {
  261.         putchar(' ') ;
  262.       }
  263.       for ( ; text < p ; text++, i++ )
  264.       {
  265.         putchar(*text) ;
  266.       }
  267.     }
  268.   }
  269. }
  270.  
  271. /*====================================================================*/
  272.